/**
 * Created by 13 on 2017/2/22.
 */
// Tags Input
$('#tags').tagsInput({
    width: '100%',
    height: '35px',
    defaultText: '请输入文章标签'
});

$('.toggle').toggles({
    on: true,
    text: {
        on: '开启',
        off: '关闭'
    }
});

$(".select2").select2({
    width: '100%'
});

var tale = new $.tale();

/**
 * 保存文章
 * @param status
 */
function subArticle(status) {
    var title = $('#articleForm input[name=title]').val();
//    var content = $('#text').val();
    var content = ue.getContent();
    var contentText = ue.getContentTxt();
    console.log("富文本内容："+content);
    console.log("简介："+$("#remark").val());
    
    if (title == '') {
        tale.alertWarn('请输入文章标题');
        return;
    }
    if (content == '') {
        tale.alertWarn('请输入文章内容');
        return;
    }
    $('#content-editor').val(content);
    $('#contentText').val(contentText);
    $("#articleForm #status").val(status);
    $("#articleForm #categories").val($('#multiple-sel').val());
    var params = $("#articleForm").serialize();
    var url = $('#articleForm #cid').val() != '' ? '/admin/article/modify' : '/admin/article/publish';
    tale.post({
        url:url,
        data:params,
        success: function (result) {
            if (result && result.success) {
                tale.alertOk({
                    text:'文章保存成功',
                    then: function () {
                        setTimeout(function () {
                            window.location.href = '/admin/article';
                        }, 500);
                    }
                });
            } else {
                tale.alertError(result.msg || '保存文章失败');
            }
        }
    });
}
//改为ueditor
//var textarea = $('#text'),
//    toolbar = $('<div class="markdown-editor" id="md-button-bar" />').insertBefore(textarea.parent())
//preview = $('<div id="md-preview" class="md-hidetab" />').insertAfter('.markdown-editor');
//
//markdown(textarea, toolbar, preview);
var ue = UE.getEditor('text',{
    initialFrameWidth:"100%",   //初始化宽度
    initialFrameHeight:400,     //初始化高度
});
//同步更新简介
ue.addListener('contentChange',function(editor){
    //相关操作
	var text = ue.getContentTxt();
	if(text.length>200){
		text = text.substring(0,200);
	}
	$("#remark").val(text);
});

function allowComment(obj) {
    var this_ = $(obj);
    var on = this_.find('.toggle-on.active').length;
    var off = this_.find('.toggle-off.active').length;
    if (on == 1) {
        $('#allowComment').val(false);
    }
    if (off == 1) {
        $('#allowComment').val(true);
    }
}

function allowPing(obj) {
    var this_ = $(obj);
    var on = this_.find('.toggle-on.active').length;
    var off = this_.find('.toggle-off.active').length;
    if (on == 1) {
        $('#allowPing').val(false);
    }
    if (off == 1) {
        $('#allowPing').val(true);
    }
}


function allowFeed(obj) {
    var this_ = $(obj);
    var on = this_.find('.toggle-on.active').length;
    var off = this_.find('.toggle-off.active').length;
    if (on == 1) {
        $('#allowFeed').val(false);
    }
    if (off == 1) {
        $('#allowFeed').val(true);
    }
}

$('div.allow-false').toggles({
    off: true,
    text: {
        on: '开启',
        off: '关闭'
    }
});

//选择封面后回显
function showCover(f) {
	var formData = new FormData(); //创建formData对象

	//判断数据 若有 则添加数据
	if(f.length > 0){
		formData.append("upfile", f[0]);//append方法用于向formdata中注入内容
	}
	
	$.ajax({
        type: "post",
        url: "/common/upload",
        async:false,
        contentType: false,
        processData: false,
        data: formData,//传过来
        dataType: 'json',
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
        	var h = "<img  width='300' height='300' id='coverImageShow' src='" + data.url + "' onClick=\"javascript:$('#coverImage').click()\" border=\"1\" title=\"请选择封面图片\"/>";
        	$("#coverImageShow")[0].outerHTML = h;
        	
        	$("#cover").val(data.url);
        }
    });
}